package com.yupfeg.test.share

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.yupfeg.livedata.R
import com.yupfeg.test.base.viewModelDelegate

/**
 * 多Fragment共享作用域Activity
 * @author yuPFeG
 * @date 2021/09/19
 */
class FragmentSharedActivity : AppCompatActivity(R.layout.activity_share_fragment){

    private val mViewModel : FragmentSharedViewModel by viewModelDelegate()

    private lateinit var mTvEventContent : TextView
    private var mNum = 0

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val senderFragment = SenderFragment()
        val observerFragment = ObserverFragment()

        supportFragmentManager.beginTransaction().apply {
            add(R.id.fragment_container_view_sender,senderFragment)
            add(R.id.fragment_container_view_observer,observerFragment)
        }.commit()

        mTvEventContent = findViewById(R.id.tv_share_activity_content)

        //订阅fragment内部的单次事件
        mViewModel.singleEvent.observe(this){event->
            mNum++
            mTvEventContent.text = "接收到Fragment发送的事件 ：${event}${mNum}"
        }
    }


}